/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fragment;

import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.table.api.TableEnvironment;

public class TchouseDeletedSink implements SqlFragment {
    @Override
    public String createConnector(ParameterTool pt, TableEnvironment tableEnv) {
        String tchouseUrl = pt.get("TCHOUSE_URL");
        String tchouseUser = pt.get("TCHOUSE_USER");
        String tchousePassword = pt.get("TCHOUSE_PASSWORD");

        String tchouseDatabase = pt.get("TCHOUSE_DATABASE");
        String tchouseSchema = pt.get("TCHOUSE_SCHEMA");
        String tchouseTable = pt.get("TCHOUSE_TABLE_PATTERN", "${table}");

        String sinkSql = String.format(
                "CREATE TABLE `sink_table_tchouse_deleted`(\n"
                        + "    `type` INT,\n"
                        + "    `phase` INT,\n"
                        + "    `data_source` STRING,\n"
                        + "    `namespace` STRING,\n"
                        + "    `schema` STRING,\n"
                        + "    `table` STRING,\n"
                        + "    `record` bytes,\n"
                        + "    `ddl` bytes,\n"
                        + "    `meta` STRING)\n"
                        + "    WITH (\n"
                        + "    'connector' = 'jdbc-inlong',\n"
                        + "    'sink.schema-change.policies' = '1=1&2=2&3=2&4=2&5=2&6=2&7=2&8=2',\n"
                        + "    'sink.schema-change.enable' = 'true',\n"
                        + "    'dirty.ignore' = 'false',\n"
                        + "    'sink.multiple.enable' = 'true',\n"
                        + "    'sink.ignore.changelog' = 'true',\n"

                        + "    'url' = '%s',\n"
                        + "    'username' = '%s',\n"
                        + "    'password' = '%s',\n"
                        + "    'sink.multiple.database-pattern' = '%s',\n"
                        + "    'sink.multiple.schema-pattern' = '%s',\n"
                        + "    'sink.multiple.table-pattern' = '%s',\n"
                        + ")", tchouseUrl, tchouseUser, tchousePassword, tchouseDatabase, tchouseSchema, tchouseTable);
        LOGGER.info("sink is \n {}", sinkSql);
        tableEnv.executeSql(sinkSql);
        return "sink_table_tchouse_deleted";
    }
}
